package com.quicks.beauty.server.service;

import com.quicks.beauty.core.dao.ActivityExpandMapper;
import com.quicks.beauty.core.dao.UserExpandMapper;
import com.quicks.beauty.core.dao.WxUserMapper;
import com.quicks.beauty.core.entity.ActivityExpand;
import com.quicks.beauty.core.entity.ActivityExpandExample;
import com.quicks.beauty.core.entity.UserExpand;
import com.quicks.beauty.core.entity.UserExpandExample;
import com.quicks.beauty.core.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServices {

    @Autowired
    private ActivityExpandMapper activityExpandMapper;

    @Autowired
    private UserExpandMapper expandMapper;

    @Autowired
    private WxUserMapper wxUserMapper;

    public ActivityExpand getOneExpand(String token) {
        ActivityExpandExample example = new ActivityExpandExample();
        ActivityExpandExample.Criteria criteria = example.createCriteria().andParamsEqualTo(token);
        List<ActivityExpand> list = activityExpandMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public int increaseExpand(String id,int num,String userId){
        ActivityExpand expand = activityExpandMapper.selectByPrimaryKey(id);
        Integer clicks = expand.getClicks();
        expand.setClicks(clicks+num);
        UserExpandExample expandExample = new UserExpandExample();
        UserExpandExample.Criteria to = expandExample.createCriteria().andUseridEqualTo(userId);
        int i = expandMapper.countByExample(expandExample);
        if (i==0) {
            UserExpand userExpand = new UserExpand();
            userExpand.setId(UUIDGenerator.generate());
            userExpand.setCreatetime(new Date());
            userExpand.setSources(expand.getParams());
            userExpand.setUserid(userId);
            expandMapper.insert(userExpand);
            Integer really = expand.getReally();
            expand.setReally(really+1);
        }
        int update = activityExpandMapper.updateByPrimaryKeySelective(expand);
        return update;
    }

    @Transactional
    public int newsIncreaseExpand(String id,String userId,int num,String userId2){
        ActivityExpand expand = new ActivityExpand();
        expand.setId(id);
        expand.setClicks(0);
        expand.setParams(userId);
        expand.setVarpath("pages/login/login?token="+userId);
        expand.setCreatetime(new Date());
        expand.setClicks(num);
        expand.setRemark(wxUserMapper.selectByPrimaryKey(userId).getNickName());
        expand.setReally(0);
        activityExpandMapper.insert(expand);
        UserExpand userExpand = new UserExpand();
        userExpand.setUserid(userId2);
        userExpand.setSources(userId);
        userExpand.setCreatetime(new Date());
        userExpand.setId(UUIDGenerator.generate());

        expandMapper.insert(userExpand);

        return 0;
    }
}
